using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;

public partial class Admin_SanPhamUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDropDownList();
        }
        //mac dinh cho ngay thang 
        txt_ngaybatdau.Text = DateTime.Now.ToString("dd/MM/yyyy");
       
    }
    private void LoadDropDownList()
    {
        tbDanhMucCollection dmCollection = tbDanhMucBUS.GetAll();
        ddl_danhmuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_danhmuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_danhmuc.DataSource = dmCollection;
        ddl_danhmuc.DataBind();
        ddl_danhmuc.Items.Insert(0, new ListItem("All", "0"));

        tbDoanhNghiepCollection dnCollection = tbDoanhNghiepBUS.GetAll();
        ddl_doanhnghiep.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_doanhnghiep.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_doanhnghiep.DataSource = dnCollection;
        ddl_doanhnghiep.DataBind();
        ddl_doanhnghiep.Items.Insert(0, new ListItem("All", "0"));

        tbNhaSXCollection nsxCollection = tbNhaSXBUS.GetAll();
        ddl_nhasanxuat.DataTextField = tbNhaSXColumns.TenNSX.ToString();
        ddl_nhasanxuat.DataValueField = tbNhaSXColumns.MaNSX.ToString();
        ddl_nhasanxuat.DataSource = nsxCollection;
        ddl_nhasanxuat.DataBind();
        ddl_nhasanxuat.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangCollection ttCollection = tbTinhTrangBUS.GetAll();
        ddl_tinhtrang.DataTextField = tbTinhTrangColumns.TenTinhTrang.ToString();
        ddl_tinhtrang.DataValueField = tbTinhTrangColumns.MaTT.ToString();
        ddl_tinhtrang.DataSource = ttCollection;
        ddl_tinhtrang.DataBind();
        ddl_tinhtrang.Items.Insert(0, new ListItem("All", "0"));

        tbDiaDiemCollection ddCollection = tbDiaDiemBUS.GetAll();
        ddl_bando.DataTextField = tbDiaDiemColumns.TenDD.ToString();
        ddl_bando.DataValueField = tbDiaDiemColumns.MaDD.ToString();
        ddl_bando.DataSource = ddCollection;
        ddl_bando.DataBind();
        ddl_bando.Items.Insert(0, new ListItem("All", "0"));

        tbPhuongThucThanhToanCollection ptttCollection = tbPhuongThucThanhToanBUS.GetAll();
        ddl_phuongthucthanhtoan.DataTextField = tbPhuongThucThanhToanColumns.TenPTTT.ToString();
        ddl_phuongthucthanhtoan.DataValueField = tbPhuongThucThanhToanColumns.MaPTTT.ToString();
        ddl_phuongthucthanhtoan.DataSource = ptttCollection;
        ddl_phuongthucthanhtoan.DataBind();
        ddl_phuongthucthanhtoan.Items.Insert(0, new ListItem("All", "0"));
       
    }
    private void LoadData()
    {
        try
        {
        int iD=int.Parse (Request .QueryString ["iD"].ToString ());
        txt_masanpham .Text =iD.ToString ();
        tbSanPham sanpham=tbSanPhamBUS .GetByID (iD);
        txt_tensanpham .Text =sanpham .TenSP ;
        ddl_danhmuc .SelectedValue = sanpham .DanhMuc.ToString () ;
        ddl_doanhnghiep .SelectedValue =sanpham .DoanhNghiep.ToString () ;
        ddl_nhasanxuat .SelectedValue =sanpham .NhaSX.ToString () ;
        txt_giagoc .Text =sanpham .GiaGoc.ToString () ;
        txt_giaban .Text =sanpham .GiaBan.ToString () ;
        ddl_bando .SelectedValue =sanpham .DiaDiem .ToString () ;
        txt_ngaybatdau .Text =sanpham .NgayBatDau .ToString ("dd/MM/yyyy");
        txt_ngayhethan .Text =sanpham .NgayHetHan .ToString ("dd/MM/yyyy");
        txt_soluongban .Text =sanpham .SoLuongBan.ToString () ;
        txt_soluongtoida .Text =sanpham .SoLuongToiDa.ToString () ;
        ftb_mota .Text =sanpham .MoTa ;
        txt_quydinhsudung .Text =sanpham .QuyDinhSuDung ;
        txt_chietkhau .Text =sanpham .ChietKhau.ToString () ;
        ddl_tinhtrang .SelectedValue =sanpham .TinhTrang.ToString () ;
        ddl_phuongthucthanhtoan.SelectedValue = sanpham.PhuongThucThanhToan.ToString();
        txt_loigioithieu.Text = sanpham.LoiGioiThieu;

         Pager.PageSize = 5;
        int total = 0;
         Criteria c = new Criteria();
        c.add (Condition .AND ,tbHinhAnhColumns .MaSP ,Condition .EQUAL ,int.Parse (txt_masanpham .Text ));
        tbHinhAnhCollection   haCollection = tbHinhAnhBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbHinhAnhColumns.MaHA.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = haCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
       }
        catch
        {
            Response .Redirect ("~/Admin/SanPhamDanhSach.aspx");
        }

    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbHinhAnh  hinhanh = (tbHinhAnh )e.Item.DataItem;
            Literal lit_MaHA = (Literal)e.Item.FindControl("lit_MaHA");
            lit_MaHA.Text = hinhanh.MaHA.ToString();
            Image img = (Image)e.Item.FindControl("hinhanh");
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            HiddenField hf_tenhinh = (HiddenField)e.Item.FindControl("hf_tenhinh");
            hf_id.Value = lit_MaHA.Text;
            hf_tenhinh.Value = hinhanh.HinhAnh.ToString();
            img.ImageUrl = "~/Admin/Photos/Thumb/" + hinhanh.HinhAnh.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_tenhinh = (HiddenField)Repeater1.Items[i].FindControl("hf_tenhinh");
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbHinhAnh  hinhanh = tbHinhAnhBUS.GetByID(iD);

                tbHinhAnhBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void btn_sua_Click(object sender, EventArgs e)
    {
        try
        {
            int iD = int.Parse(Request.QueryString["iD"].ToString());
            tbSanPham sanpham = tbSanPhamBUS.GetByID(iD);
            sanpham.TenSP = txt_tensanpham.Text;
            sanpham.DanhMuc =int.Parse ( ddl_danhmuc.SelectedValue);
            sanpham.DoanhNghiep = int.Parse (ddl_doanhnghiep.SelectedValue);
            sanpham.NhaSX =int.Parse ( ddl_nhasanxuat.SelectedValue);
            sanpham.GiaGoc = int.Parse (txt_giagoc.Text);
            sanpham.GiaBan = int.Parse (txt_giaban.Text);
            sanpham.DiaDiem = int.Parse (ddl_bando.SelectedValue);
            sanpham.NgayBatDau = DateTime.Parse(txt_ngaybatdau.Text);
            sanpham.NgayHetHan = DateTime.Parse(txt_ngayhethan.Text);
            sanpham.SoLuongBan =int.Parse ( txt_soluongban.Text);
            sanpham.SoLuongToiDa =int.Parse (txt_soluongtoida.Text);
            sanpham.MoTa = ftb_mota.Text;
            sanpham.QuyDinhSuDung = txt_quydinhsudung.Text;
            sanpham.ChietKhau =int.Parse ( txt_chietkhau.Text);
            sanpham.TinhTrang =int.Parse ( ddl_tinhtrang.SelectedValue);
            sanpham.PhuongThucThanhToan = int.Parse(ddl_phuongthucthanhtoan.SelectedValue);
            sanpham.LoiGioiThieu = txt_loigioithieu.Text;
            tbSanPhamBUS.Update(sanpham);

            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    //hpf.SaveAs(Server.MapPath("Photos") + "_" + Path.GetFileName(hpf.FileName));
                    string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~/Admin/Photos/" + filename));

                    System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Admin/Photos/" + filename));
                    ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Admin/Photos/Thumb/"), filename);

                    tbHinhAnh ha = new tbHinhAnh();
                    ha.MaSP = iD;
                    ha.HinhAnh = filename;
                    tbHinhAnhBUS.Insert(ha);

                }
            }
        }
        catch
        {
            
        }
        Response.Redirect("~/Admin/SanPhamDanhSach.aspx");
        Response.Redirect(Request.RawUrl);
    }
}

